package com.apes.fa.model;

import com.apes.fa.respository.FaCardAccountRepository;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import com.apes.scm.masterdata.product.model.FaCategory;
import com.apes.scm.masterdata.product.model.FaDepreciationMethod;
import com.apes.scm.masterdata.product.model.Product;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;
import java.util.Objects;


/**
 * @description: 资产卡片
 * @author: PengZiFeng
 * @create: 2021-08-19 17:11
 **/
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = FaCard.CONST_TABLE_NAME)
public class FaCard extends Model {

    public static final String CONST_TABLE_NAME = "fa_card";

    @Id
    @Column(name = "id", nullable = false, length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "资产卡片", prefix = "%(faCateIDPrefix)%(company)", padding = 5)
    @Comment("资产卡片编码")
    private String id;

    @Column(name = "fa_id")
    @Comment("固定资产编码")//同资产卡片编码
    private String faId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fa_category_id", referencedColumnName = "id")
    @Comment("固定资产分类编码")
    private FaCategory faCategory;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司")
    private PartyRole company;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id")
    @Comment("折旧部门")
    private Dept dept;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "use_dept_id", referencedColumnName = "id")
    @Comment("使用部门")
    private Dept useDept;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", referencedColumnName = "id")
    @Comment("商品名称")
    private Product product;

    @Column(name = "purchase_date")
    @Comment("购置日期")
    private Date purchaseDate;

    @Column(name = "quantity")
    @Comment("数量")
    private Integer quantity;

    @Column(name = "book_value")
    @Comment("账面价值")
    private Double bookValue;

    @Column(name = "original_value")
    @Comment("原值")
    private Double originalValue;

    @Column(name = "depreciation")
    @Comment("折旧")
    private Double depreciation;

    @Column(name = "enable_date")
    @Comment("启用日期")
    private Date enableDate;

    @Column(name = "use_year")
    @Comment("使用年限")
    private Integer useYear;

    @Column(name = "use_month")
    @Comment("使用月限")
    private Integer useMonth;

    @Column(name = "estimated_residual_value_rate")
    @Comment("预计残值率")
    private Double estimatedResidualValueRate;

    @ManyToOne
    @JoinColumn(name = "depreciation_method_id",referencedColumnName = "id")
    @Comment("折旧方法")
    private FaDepreciationMethod depreciationMethod;

    @Column(name = "annual_depreciation_rate")
    @Comment("年折旧率")
    private Double annualDepreciationRate;

    @Column(name = "monthly_depreciation_rate")
    @Comment("月折旧率")
    private Double monthlyDepreciationRate;

    @Column(name = "fa_status")
    @Comment("资产状态")
    @States(name = "资产状态", values = {
            @State(id = "1", name = "在用"),
            @State(id = "2", name = "闲置"),
            @State(id = "3", name = "维修"),
            @State(id = "4", name = "借出"),
            @State(id = "5", name = "报废"),
            @State(id = "6", name = "售出"),
            @State(id = "7", name = "盘亏待处理"),
            @State(id = "8", name = "盘亏")
    })
    //资产状态(1:在用;2:闲置;3:维修;4:借出;5:报废;6:调拨售出;7:盘亏待处理;8:盘亏)
    private String faStatus;

    @Column(name = "fa_type")
    @Comment("资产类型")
    private String faType;

    @Column(name = "fa_source")
    @Comment("资产来源")
    private String faSource;

    @Column(name = "fa_whereabouts")
    @Comment("资产去向")
    private String faWhereabouts;

    @Column(name = "transfer_in_unit")
    @Comment("转入单位")
    private String transferInUnit;

    @Column(name = "transfer_unit")
    @Comment("转出单位")
    private String transferUnit;

    @Column(name = "evaluation_mark")
    @Comment("评估标志")
    private String evaluationMark;

    @Column(name = "evaluation_original_value")
    @Comment("评估原值")
    private Double evaluationOriginalValue;

    @Column(name = "depreciation_mark",nullable = false,  columnDefinition = "number(1,0) default 1")
    @Comment("折旧标记")
    private boolean depreciationMark = true;

    @Column(name = "measurement_unit")
    @Comment("计量单位")
    private String measurementUnit;

    @Column(name = "business_name")
    @Comment("单据名称")
    private String businessName;

    @Column(name = "source_business_type")
    @Comment("来源单据类型")
    private String sourceBusinessType;

    @Column(name = "source_business_name")
    @Comment("来源单据名称")
    private String sourceBusinessName;

    @Column(name = "source_business_key")
    @Comment("来源单据号码")
    private String sourceBusinessKey;

    @Column(name = "remaining_depreciation_month")
    @Comment("剩余折旧月")
    private Integer remainingDepreciationMonth;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "responsible_person_id", referencedColumnName = "id")
    @Comment("负责人")
    private PartyRole responsiblePerson;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "custodian_id", referencedColumnName = "id")
    @Comment("保管人")
    private PartyRole custodian;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", referencedColumnName = "id")
    @Comment("使用人")
    private PartyRole user;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "manage_person_id", referencedColumnName = "id")
    @Comment("管理人")
    private PartyRole managePerson;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审批人")
    private PartyRole approveUid;

    @Column(name = "approve_date")
    @Comment("审批日期")
    private Date approveDate;

    @Column(name = "scrap_date")
    @Comment("报废日期")
    private Date scrapDate;

    @Column(name = "business_state")
    @Comment("单据状态")
    private String businessState;

    @Column(name = "note")
    @Comment("备注")
    private String note;

    @Column(name = "use_place")
    @Comment("使用地点")
    private String usePlace;

    @Column(name = "state")
    @Comment("状态(sent:创建,approve:审核)")
    private String state;

    @Transient
    private PartyRole _creator;

    @Transient
    public PartyRole get_creator() {
        if(this.getCreator() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getCreator());
    }

    //ID前缀
    @Transient
    private String faCateIDPrefix;

    public String getFaCateIDPrefix(){
        return faCategory.getId().substring(0,3);
    }

    //成本中心名称 取数折旧部门
    @Transient
    private String costCenterName;

    @Transient
    public String getCostCenterName() {
        if(this.dept.getCostCenterId() == null) return null;
        return this.dept.getCostCenterId().getName();
    }

    @Transient
    private Integer flag;

    public Integer getFlag(){
        if(Objects.isNull(SpringManager.getBean(FaCardAccountRepository.class).findAllByFaCardId(id))){
            return 0;
        }else {
            return 1;
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        FaCard faCard = (FaCard) o;
        return id.equals(faCard.id);
    }
}
